//
//  NSStringAdditions.m
//  YinXinBao
//
//  Created by caoliang on 14-4-14.
//  Copyright (c) 2014年 UMPAY. All rights reserved.
//

#include <dirent.h>
#include <sys/stat.h>
#import <CommonCrypto/CommonDigest.h>

#import "NSStringAdditions.h"

@implementation NSString(NSStringAdditions)

+ (NSString *) returnHourAndMinute:(NSUInteger) defaultValue{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"HH:mm"];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:defaultValue];
    return [formatter stringFromDate:date];
}

+ (NSString *) returnDayAndHour:(NSUInteger) defaultValue{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];// HH:mm
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:defaultValue];
    return [formatter stringFromDate:date];
}

+ (NSString *) returnDayAndHourAndMinute:(NSUInteger) defaultValue{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];// HH:mm
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:defaultValue];
    return [formatter stringFromDate:date];
}

+(NSString *)getTimeValueForKey:(NSString *)key defaultValue:(time_t)defaultValue {
	NSString *stringTime   = key;
    time_t timeValue;
    NSString *_timestamp;
    if ((id)stringTime == [NSNull null]) {
        stringTime = @"";
    }
	struct tm created;
    time_t now;
    time(&now);
    
	if (stringTime) {
		if (strptime([stringTime UTF8String], "%a %b %d %H:%M:%S %z %Y", &created) == NULL) {
			strptime([stringTime UTF8String], "%a, %d %b %Y %H:%M:%S %z", &created);
		}
		timeValue = mktime(&created);
	}else{
        timeValue = defaultValue;
    }

    int distance = (int)difftime(now, timeValue);
    if (distance < 0) distance = 0;
    
    if (distance < 60) {
        _timestamp = [NSString stringWithFormat:@"%d%@", distance, (distance == 1) ? @"秒前" : @"秒前"];
    }
    else if (distance < 60 * 60) {
        distance = distance / 60;
        _timestamp = [NSString stringWithFormat:@"%d%@", distance, (distance == 1) ? @"分钟前" : @"分钟前"];
    }
    else if (distance < 60 * 60 * 24) {
        distance = distance / 60 / 60;
        _timestamp = [NSString stringWithFormat:@"%d%@", distance, (distance == 1) ? @"小时前" : @"小时前"];
    }
    else if (distance < 60 * 60 * 24 * 7) {
        distance = distance / 60 / 60 / 24;
        _timestamp = [NSString stringWithFormat:@"%d%@", distance, (distance == 1) ? @"天前" : @"天前"];
    }
    else if (distance < 60 * 60 * 24 * 7 * 4) {
        distance = distance / 60 / 60 / 24 / 7;
        _timestamp = [NSString stringWithFormat:@"%d%@", distance, (distance == 1) ? @"周前" : @"周前"];
    }
    else {
        static NSDateFormatter *dateFormatter = nil;
        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateStyle:NSDateFormatterShortStyle];
            [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        }
        
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeValue];
        _timestamp = [dateFormatter stringFromDate:date];
    }
    return _timestamp;
}

+(NSString *)getVideoTimeValueForKey:(NSString *)key{
    return [NSString stringWithFormat:@"%02d:%02d:%02d",(key.integerValue / 3600), (key.integerValue % 3600 /60),(key.integerValue % 60)];
}

+(NSString *)getTimeValueForInt:(int)nValue{
    return [NSString stringWithFormat:@"%02d:%02d:%02d",(nValue / 3600), (nValue % 3600 /60),(nValue % 60)];
}


+ (NSString *)daySinceDate:(NSDate *) currentDate{
    NSTimeInterval time = [currentDate timeIntervalSince1970];
    return [self daySinceTimeInterval:time];
}

+ (NSString *) dayManageTimeInterval:(NSString *) time{
    NSString * timeStr = [time stringByReplacingOccurrencesOfString:@"/Date(" withString:@""];
    timeStr = [timeStr stringByReplacingOccurrencesOfString:@")/" withString:@""];
    
   return timeStr;
}

+ (NSString *)daySinceTimeInterval:(NSTimeInterval)time {
    NSString *addtimeDisplay = @"";
    NSInteger minute = 60;
    NSInteger hour = minute * 60;
    NSInteger day = hour * 24;
    NSInteger now = ceil([[NSDate date] timeIntervalSince1970]);
    NSInteger timeDifference = now-time;
    if (timeDifference > day) {
        int num = timeDifference/60/60/24;
        addtimeDisplay = [NSString stringWithFormat:@"%d 以前",num];
    }else if(timeDifference > hour) {
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
        NSDate *now;
        NSDateComponents *comps;
        NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
        NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        now=[NSDate date];
        comps = [calendar components:unitFlags fromDate:now];
        NSInteger tHour = [comps hour];
        NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:time];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        if (timeDifference/60/60 > tHour) {
            addtimeDisplay = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:date]];
        }else{
            addtimeDisplay = [NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:date]];
        }
    }else{
        NSInteger minutes = timeDifference/minute;
        if (minutes < 3) {
            addtimeDisplay = @"刚刚";
        }else{
            addtimeDisplay = [NSString stringWithFormat:@"%d 前",minutes];
        }
    }
    return addtimeDisplay;
}

+ (NSString *) changeDistanceStr:(NSInteger) aDistance{
    if (aDistance >= 1000){
        return [NSString stringWithFormat:@"%.1f公里", (float)aDistance/ 1000];
    }else{
        return [NSString stringWithFormat:@"%d米", aDistance];
    }
}


@end
